package cn.gson.crm.controller.relation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.crm.model.dao.NewTechnologyDao;
import cn.gson.crm.model.entity.WhNewTechnology;
import cn.gson.crm.model.mapper.NewTechnologyMapper;

@Controller
@RequestMapping("/newTechnology")
public class NewTechnologyController {

	@Autowired
	private NewTechnologyDao ntd;
	
	@Autowired
	private NewTechnologyMapper ntm;
	
	/**
	 * 查询所有学员的就业反馈信息
	 * @param stuinfor
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Map<String,Object> index(WhNewTechnology wnt, @RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize){
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<WhNewTechnology> pageData = ntm.list(wnt);

		PageInfo<WhNewTechnology> info = new PageInfo<>(pageData);
		// 组装返回结果
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	
	/**
	 * 删除新兴技术记录
	 * @param id
	 * @return
	 */
	@GetMapping("delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String,Object> delete(@PathVariable("id") Long id) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		WhNewTechnology d;
		if (id != null && (d = ntd.findOne(id)) != null) {
			d.setDelete(0L);
			obj.put("success", true);
		} else {
			obj.put("message", "抱歉，还未上传该技术");
		}
		return obj;
	}
	
	/**
	 * 将学员信息放在关系维护表单上
	 * @param model
	 * @return
	 */
	@GetMapping({ "add_new_technology", "add_new_technology/{id}" })
	public String form(@PathVariable(name = "id", required = false) Long id, Model model) {
		if (id != null) {
			//根据Id查询就业学员的反馈信息
			WhNewTechnology wnt = ntd.findOne(id);
			//查询所有就业学员的姓名和id
			if (wnt != null) {
				// 如果是编辑，则把原有用户信息传回表单页面
				model.addAttribute("wnt", wnt);
			}
		}
		List<Map<String, Object>> stu = ntm.selectAllStudent();
		model.addAttribute("students", stu);
		return "newTechnology/add_new_technology";
	}
	
	/**
	 * 添加和编辑
	 * @param stuinfor
	 * @param br
	 * @return
	 */
	@PostMapping("save")
	@ResponseBody
	@Transactional
	public Map<String,Object> save(@Valid WhNewTechnology wnt, BindingResult br) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
			if (wnt.getStudent().getId() == null) {	
					obj.put("message", "该用户不存在！");
					return obj;
					
			} else {
				int count=ntm.countTechniqueName(wnt.getTechniqueName());
				if(wnt.getId()==null) {
					
					if (count>=1) {
							obj.put("message", "该技术已存在");
							return obj;
						}
				}else {
					if (count>1) {
						obj.put("message", "该技术已存在");
						return obj;
					}
				}
				wnt.setDelete(1L);
				ntd.save(wnt);
				obj.put("success", true);

			}
		
		return obj;
	}
	
}
